package com.th.apicenter.kuaishou;

import com.th.apicenter.baidu.service.BaiduService;
import com.th.apicenter.kuaishou.vo.KsCallbackVO;
import com.th.apicenter.mi.service.MiService;
import com.th.apicenter.toutiao.service.ToutiaoService;
import com.th.apicenter.vivo.service.VIVOService;
import com.th.apicenter.xinlang.service.XinlangService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/ks")
@Slf4j
public class KuaiShouController {

    @Autowired
    private XinlangService xinlangService;

    @Autowired
    private MiService miService;

    @Autowired
    private VIVOService vivoService;

    @Autowired
    private ToutiaoService toutiaoService;

    @Autowired
    private BaiduService baiduService;

    /**
     * 1.新增
     * 2.次日留存
     * 3.活跃
     * 4.流失回流
     *
     * @param actionType
     * @return
     */
    @GetMapping("/xl/callback")
    public KsCallbackVO ksxlCallback(@RequestParam(name = "actionType", required = false) String actionType,
                                     @RequestParam(name = "clickId", required = false) String clickId) {
        log.info("新浪上传数据接收到快手的回调---------------------------------------------------------------");
        log.info("接收到快手的回调actionType为：{}", actionType);
        log.info("接收到快手的回调clickId为：{}", clickId);

        xinlangService.callback(clickId, actionType);

        KsCallbackVO ksCallbackVO = new KsCallbackVO();
        ksCallbackVO.setRet(0);
        ksCallbackVO.setMsg("");
        ksCallbackVO.setData(null);
        return ksCallbackVO;
    }

    /**
     * 1.新增
     * 2.次日留存
     * 3.活跃
     * 4.流失回流
     *
     * @param actionType
     * @return
     */
    @GetMapping("/xl/callback_active")
    public KsCallbackVO ksxlCallback_active(@RequestParam(name = "actionType", required = false) String actionType,
                                            @RequestParam(name = "clickId", required = false) String clickId) {
        log.info("新浪上传数据接收到快手的回调---------------------------------------------------------------");
        log.info("接收到快手的回调actionType为：{}", actionType);
        log.info("接收到快手的回调clickId为：{}", clickId);

        xinlangService.callback_active(clickId, actionType);

        KsCallbackVO ksCallbackVO = new KsCallbackVO();
        ksCallbackVO.setRet(0);
        ksCallbackVO.setMsg("");
        ksCallbackVO.setData(null);
        return ksCallbackVO;
    }

    @GetMapping("/mi/callback")
    public KsCallbackVO ksmiCallback(@RequestParam(name = "actionType", required = false) String actionType,
                                     @RequestParam(name = "MiId", required = false) String MiId) {

        log.info("小米上传数据接收到快手的回调---------------------------------------------------------------");
        log.info("接收到快手的回调actionType为：{}", actionType);
        log.info("接收到快手的回调MiId为：{}", MiId);

        miService.callback(actionType, MiId);

        KsCallbackVO ksCallbackVO = new KsCallbackVO();
        ksCallbackVO.setRet(0);
        ksCallbackVO.setMsg("");
        ksCallbackVO.setData(null);
        return ksCallbackVO;
    }


    @GetMapping("/vivo/callback")
    public KsCallbackVO ksvivoCallback(@RequestParam(name = "actionType", required = false) String actionType,
                                       @RequestParam(name = "VivoId", required = false) String VivoId) {

        log.info("vivo上传数据接收到快手的回调---------------------------------------------------------------");
        log.info("接收到快手的回调actionType为：{}", actionType);
        log.info("接收到快手的回调VivoId为：{}", VivoId);

        try {
            vivoService.callback(actionType, VivoId);
        } catch (IOException e) {
            e.printStackTrace();
        }

        KsCallbackVO ksCallbackVO = new KsCallbackVO();
        ksCallbackVO.setRet(0);
        ksCallbackVO.setMsg("");
        ksCallbackVO.setData(null);
        return ksCallbackVO;
    }


    @GetMapping("/toutiao/callback")
    public KsCallbackVO kstoutiaoCallback(@RequestParam(name = "actionType", required = false) String actionType,
                                          @RequestParam(name = "TTId", required = false) String TTId) {
        try {
            toutiaoService.callback(actionType, TTId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        KsCallbackVO ksCallbackVO = new KsCallbackVO();
        ksCallbackVO.setRet(0);
        ksCallbackVO.setMsg("");
        ksCallbackVO.setData(null);
        return ksCallbackVO;
    }

    @GetMapping("/baidu/callback")
    public KsCallbackVO ksbaiduCallback(@RequestParam(name = "actionType", required = false) String actionType,
                                        @RequestParam(name = "baiduId", required = false) String baiduId) {

        baiduService.callback(actionType, baiduId);
        KsCallbackVO ksCallbackVO = new KsCallbackVO();
        ksCallbackVO.setRet(0);
        ksCallbackVO.setMsg("");
        ksCallbackVO.setData(null);
        return ksCallbackVO;
    }

}
